package com.bkhc.product.repository;

import com.bkhc.product.entity.GoodsSpec;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface GoodspecRepository extends JpaRepository<GoodsSpec,String> , JpaSpecificationExecutor<GoodsSpec> {

    GoodsSpec findByGoodsCodeAndSpecCode(String goodsCode, String specCode);

    @Query("select g from GoodsSpec g left join Spec s on g.specCode = s.specCode where g.goodsCode = :goodsCode order by s.sort")
    List<GoodsSpec> findByGoodsCode(String goodsCode);

    @Query("SELECT g FROM GoodsSpec  g INNER JOIN Spec s ON g.specCode = s.specCode WHERE g.goodsCode in (:goodsCodes) ORDER BY g.goodsCode,s.sort ASC")
    List<GoodsSpec> findByGoodsCodeIn(List<String> goodsCodes);

    @Query("delete from GoodsSpec gs where gs.goodsCode in (select g.goodsCode from Goods g where g.productCode =:productCode)")
    @Modifying
    void deleteByProductCode(@Param("productCode") String productCode);
}
